//获取章节信息

import axios from "axios";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { getCurrentInstance } from "vue";

export async function addHighLight( book_id, chapter_id, start_pos, end_pos, color ,proxy) {
  console.log( book_id, chapter_id, start_pos, end_pos, color);
  try {
    const formData = new FormData();
    formData.append("book_id", book_id);
    formData.append("chapter_id", chapter_id);
    formData.append("start_pos", start_pos);    
    formData.append("end_pos", end_pos);
    formData.append("color", color);
    const response = await proxy.$http.post(
      "/api/book/add-highlight",
      formData,
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    );
    const res = response.data.data;
    console.log(response);
    const isSuccess = response.data.success;
    const message = response.data.message;
    if (isSuccess === false) {
      ElMessage.error(message);
    } else {
      return true;
    }
  } 
  catch (error) {
    console.error(`Error fetching chapter:`, error);
    return [];
  }
}

export async function deleteHighLights(highlightId,proxy) {
  const formData = new FormData();
  formData.append("highlight_id", highlightId);
  try {
    const response = await proxy.$http.post(
      "/api/book/cancel-highlight",
      formData,
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }
    );
      const isSuccess = response.data.success;
      const message = response.data.message;
      if (isSuccess === false) {
          ElMessage.error(message);
      } else {
          return true;
      }
    } catch (error) {
        console.error(`Error delete ${highlightId} hightlight:`, error);
        return [];
    }
}